package com.xckj.cycling.utils;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;

import androidx.annotation.Nullable;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * bitmap相关方法
 *
 * @author liyang
 */
public class BitmapUtils {


    /**
     * 垂直平铺bitmap
     *
     * @param height 内容高度
     * @param src    图片
     * @return 铺满的bitmap
     */
    public static Bitmap createRepeater(int height, Bitmap src) {
        int count = (height + src.getHeight() - 1) / src.getHeight();
        Bitmap bitmap = Bitmap.createBitmap(src.getWidth(), src.getHeight() * count, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        for (int idx = 0; idx < count; ++idx) {
            canvas.drawBitmap(src, 0, idx * src.getHeight(), null);
        }
        return bitmap;
    }

    /**
     * 水平镜像图片
     *
     * @param height 内容高度
     * @param bmp    图片
     * @return 铺满的水平镜像bitmap
     */
    public static Bitmap toHorizontalMirror(int height, Bitmap bmp) {
        int w = bmp.getWidth();
        int h = bmp.getHeight();
        Matrix matrix = new Matrix();
        matrix.postScale(-1f, 1f);
        Bitmap bitmap = Bitmap.createBitmap(bmp, 0, 0, w, h, matrix, true);
        return createRepeater(height, bitmap);
    }

    /**
     * bitmap 切圆
     *
     * @param bitmap 原始bitmap
     * @return 圆形bitmap
     */
    public static Bitmap makeRoundCorner(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int left = 0, top = 0, right = width, bottom = height;
        float roundPx = height / 2;
        if (width > height) {
            left = (width - height) / 2;
            top = 0;
            right = left + height;
            bottom = height;
        } else if (height > width) {
            left = 0;
            top = (height - width) / 2;
            right = width;
            bottom = top + width;
            roundPx = width / 2;
        }
        Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        int color = 0xff424242;
        Paint paint = new Paint();
        Rect rect = new Rect(left, top, right, bottom);
        RectF rectF = new RectF(rect);

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }

    /**
     * 生成纯色bitmap
     *
     * @param width  bitmap 宽
     * @param height bitmap 高
     * @return bitmap
     */
    public static Bitmap getBitmapByColor(int width, int height) {
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        int[] pixels = new int[width * height];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                pixels[i * width + j] = Color.WHITE;
            }
        }
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    }

    /**
     * bitmap合成
     *
     * @param bitmap1 bitmap1
     * @param bitmap2 bitmap2
     * @return 合成的bitmap
     */
    public static Bitmap composeBitmap(Bitmap bitmap1, Bitmap bitmap2) {
        int bgWidth = bitmap1.getWidth();
        int bgHeight = bitmap1.getHeight();
        Bitmap newBitmap = Bitmap.createBitmap(bgWidth, bgHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(newBitmap);
        canvas.drawBitmap(bitmap1, 0, 0, null);
        canvas.drawBitmap(bitmap2, (bgWidth - bitmap2.getWidth()) >> 1, (bgHeight - bitmap2.getHeight()) >> 1, null);
        canvas.save();
        canvas.restore();
        return newBitmap;
    }

    /**
     * 返回默认中间拉伸的.9图片的Chunk
     *
     * @param bitmap
     * @return
     */
    public static byte[] makeNinePatchChunk(Bitmap bitmap) {
        return makeNinePatchChunk(bitmap.getWidth(), bitmap.getHeight());
    }

    /**
     * @param width  bitmap的宽
     * @param height bitmap的高
     * @return
     */
    public static byte[] makeNinePatchChunk(int width, int height) {
        int[] xRegions = new int[]{width / 2, width / 2 + 1};
        int[] yRegions = new int[]{height / 2, height / 2 + 1};
        int NO_COLOR = 0x00000001;
        int colorSize = 9;
        int bufferSize = xRegions.length * 4 + yRegions.length * 4 + colorSize * 4 + 32;

        ByteBuffer byteBuffer = ByteBuffer.allocate(bufferSize).order(ByteOrder.nativeOrder());
        // 第一个byte，要不等于0
        byteBuffer.put((byte) 1);

        //mDivX length
        byteBuffer.put((byte) 2);
        //mDivY length
        byteBuffer.put((byte) 2);
        //mColors length
        byteBuffer.put((byte) colorSize);

        //skip
        byteBuffer.putInt(0);
        byteBuffer.putInt(0);

        //padding 先设为0
        byteBuffer.putInt(0);
        byteBuffer.putInt(0);
        byteBuffer.putInt(0);
        byteBuffer.putInt(0);

        //skip
        byteBuffer.putInt(0);

        // mDivX
        byteBuffer.putInt(xRegions[0]);
        byteBuffer.putInt(xRegions[1]);

        // mDivY
        byteBuffer.putInt(yRegions[0]);
        byteBuffer.putInt(yRegions[1]);

        // mColors
        for (int i = 0; i < colorSize; i++) {
            byteBuffer.putInt(NO_COLOR);
        }

        return byteBuffer.array();
    }

    @Nullable
    public static Bitmap mirror(@Nullable Bitmap bitmap) {
        if (bitmap == null) {
            return null;
        }
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Bitmap result = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(result);
        Matrix matrix = new Matrix();
        matrix.postScale(-1, 1);
        Bitmap result2 = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
        canvas.drawBitmap(result2, new Rect(0, 0, w, h), new Rect(0, 0, w, h), null);
        return result;
    }
}
